'''
Created on Feb 28, 2014

@author: Antoine
'''
from Tkconstants import FIRST

''' ****************************************** '''
''' **************** imports ***************** '''
''' ****************************************** '''
import nltk
from nltk.tokenize import regexp_tokenize
from nltk import load_parser


''' ****************************************** '''
''' *************** Functions **************** '''
''' ****************************************** '''

def readFile():
    myFile = open('phrases.txt')
    sentences = []
    
    for line in myFile :
        sentences.append(line)
        
    myFile.close()       
    return sentences


def writeFile(answers):
    myFile = open('answers.txt', "w")
    
    for answer in answers :
        myFile.write(str(answer) + "\n")
        
    myFile.close()

''' ****************************************** '''
''' ************** Pre-process *************** '''
''' ****************************************** '''

indexVerbeAction = 0
indexDeQui = 1
indexDeQuoi = 2

cp = load_parser('file:maGrammaire.fcfg')
pattern = r'\w+|[^\w\s]+'

sentences = readFile()

firstOrderLogicAnswers = []
folAnswerNotTransformed = [[]]
folAnswerTransformed = [[]]

for line in sentences :
    tokens = regexp_tokenize(line, pattern)
    
    trees = cp.nbest_parse(tokens)

    ''' Post-process '''  
        
    ''' Si il y a plusieurs arbres pour une meme phrase, alors la grammaire est ambigue '''    
    for tree in trees: 
        tree.draw()
    
        if (trees <> []) :
            firstOrderLogicAnswer = str(trees[0].node['SEM'])
            firstOrderLogicAnswers.append(str(firstOrderLogicAnswer))
        
        for folAnswer in firstOrderLogicAnswers :
            partitionSentenceList = list(str(folAnswer).partition("&"))
            
            firstSentence = str(partitionSentenceList[0])   
            firstSentence = firstSentence.replace("(", ",").replace(")", "")        
            
            if (str(folAnswer).find("&") <> -1) :
                firstSentence = firstSentence[1:]
                secondSentence = str(partitionSentenceList[2])
                secondSentence = secondSentence.replace("(", ",").replace(")", "")
    
            folAnswerNotTransformed.append(firstSentence.strip().split(","))
            
            if (secondSentence) :
                folAnswerNotTransformed.append(secondSentence.strip().split(","))
                
            previousJessAnswer = []
                
            for actualJessAnswer in folAnswerNotTransformed :
                if (actualJessAnswer <> []) :
                    
                    if (str(actualJessAnswer).lower().find("elle") <> -1) :
                        actualJessAnswer[indexDeQui] = previousJessAnswer[indexDeQui]
                        
                    elif (str(actualJessAnswer).lower().find("il") <> -1) :
                        actualJessAnswer[indexDeQui] = previousJessAnswer[indexDeQui]

                    folAnswerTransformed.append(actualJessAnswer)
                        
                    previousJessAnswer = actualJessAnswer
                    
        folAnswerWithoutDuplicates = [[]]
        
        for i in folAnswerTransformed:
            if i not in folAnswerWithoutDuplicates:
                folAnswerWithoutDuplicates.append(i)
                
    jessAnswers = []
            
    for folAnswer in folAnswerWithoutDuplicates :
        if (folAnswer <> []) :
            if (str(folAnswer[indexVerbeAction]).find("-") > -1) :
                jessAnswers.append("personnage " + folAnswer[indexDeQui] + " " + str(folAnswer[indexVerbeAction]).replace("-", "") + " aucun " + folAnswer[indexDeQuoi])
            else :
                jessAnswers.append("personnage " + folAnswer[indexDeQui] + " " + folAnswer[indexVerbeAction] + " " + folAnswer[indexDeQuoi])
                
    writeFile(jessAnswers)
                